var Users = require('./users').Users;

var Insert = function(user_id, user_key, user_name) {
	var _userid = {userid: user_id};
	var update = {userkey: user_key, username: user_name, time: new Date};
	var options    = {upsert : true};
	var callback = function(err) {
		if(err) {
			console.log('insert err:' + err);
		} else {
			console.log('insert success');
		}
	}

	Users.Insert(_userid, update, options, callback);
}

var Delete = function(user_id) {
	var _userid = {userid: user_id};
	var callback = function(err) {
		if(err) {
			console.log('delete err:' + err);
		} else {
			console.log("delete success");
		}
	}
	Users.Delete(_userid, callback);
}

var Update = function(user_id, user_key, user_name) {
	var _userid = {userid: user_id};
	var update = {userkey: user_key, username: user_name, time: new Date};
	var options    = {upsert : true};
	var callback = function(err) {
		if(err) {
			console.log('update err:' + err);
		} else {
			console.log('update success');
		}
	}

	Users.Update(_userid, update, options, callback);
}

var Query = function(user_id) {
	var _userid = {userid: user_id};
	var callback = function(err, user) {
		if(err) {
			console.log('query err:' + err);
		} else {
			console.log('query success, user:' + user);
			console.log('user.userkey:' + user.userkey);
		}
	}
	new Users({}).Query(_userid, callback);
}


exports.Insert = Insert;
exports.Delete = Delete;
exports.Update = Update;
exports.Query = Query;

